package com.example.networktestbyshell;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


//id-111,num-4,devices-9607,url-baidu.com
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private final static String TAG = "NetworkTest";
    private static Button btn_660, btn_9607;
    private static TextView TV660, TV9607;
    private static EditText mCmdText, urlFor660, urlFor9607;
    private static Context mContext;
    private static boolean m660IsFinish = true;
    private static boolean m9607IsFinish = true;


    private String ip;
    private int port;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mContext = this;
        setContentView(R.layout.activity_main);
        btn_660 = (Button)findViewById(R.id.button_660);
        btn_9607 = (Button)findViewById(R.id.button_9607);


        TV660 = (TextView) findViewById(R.id.pingTextView660);
        TV9607 = (TextView) findViewById(R.id.pingTextView9607);

        btn_660.setOnClickListener(this);
        btn_9607.setOnClickListener(this);

        mCmdText = (EditText)findViewById(R.id.pwdText);
        urlFor660 = (EditText)findViewById(R.id.urlfor660);
        urlFor9607 = (EditText)findViewById(R.id.urlfor9607);
        connect("127.0.0.1", Integer.parseInt("9999"));

        while (true){
            if (TcpClient.getInstance().isConnect()) {
                //byte[] data = mCmdText.getText().toString().getBytes();
                String cmd1 = "id-111,num-4,devices-660,url-baidu.com";
                send(cmd1.getBytes());
                Log.d(TAG, "send : " + cmd1);
                String cmd2 = "id-111,num-4,devices-9607,url-baidu.com";
                send(cmd2.getBytes());
                Log.d(TAG, "send : " + cmd2);
                break;
            }
        }
    }

    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.button_660:
                if (m660IsFinish) {
                    String url = urlFor660.getText().toString().isEmpty()?  "baidu.com" : urlFor660.getText().toString();
                    String cmd = "id-222,num-30,devices-660,url-" + url;
                    sendCMD(cmd);
                    m660IsFinish = false;
                    Log.d(TAG, "cmd onclick " + cmd);
                } else {
                    Toast.makeText(this,"660 ping hasn't completed", Toast.LENGTH_SHORT).show();
                }


                break;
            case R.id.button_9607:

                if (m9607IsFinish) {
                    String url = urlFor9607.getText().toString().isEmpty()?  "baidu.com" : urlFor9607.getText().toString();
                    String cmd = "id-222,num-30,devices-9607,url-" + url;
                    sendCMD(cmd);
                    m9607IsFinish = false;
                    Log.d(TAG, "cmd onclick " + cmd);
                } else {
                    Toast.makeText(this,"9607 ping hasn't completed", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    public void sendCMD(String cmd) {
        if (TcpClient.getInstance().isConnect()) {
            //byte[] data = mCmdText.getText().toString().getBytes();

            send(cmd.getBytes());
            Log.d(TAG, "send : " + mCmdText.getText().toString());
        } else {
            Toast.makeText(this,"尚未连接，请连接Socket",Toast.LENGTH_SHORT).show();
        }

    }


    /**
     * socket data receive
     * */
    private void initDataReceiver(){
        TcpClient.getInstance().setOnDataReceiveListener(dataReceiveListener);
    }

    /**
     * socket connect
     * */
    private void connect(String ip, int port){
        TcpClient.getInstance().connect(ip, port);
    }

    /**
     * socket disconnect
     * */
    private void disconnect(){
        TcpClient.getInstance().disconnect();
      //  tv_state.setText("未连接");
    }

    /**
     * socket send
     * */
    private void send(byte[] data){
        TcpClient.getInstance().sendByteCmd(data,1001);
    }

    /**
     * socket data receive
     * data(byte[]) analyze
     * */
    private TcpClient.OnDataReceiveListener dataReceiveListener = new TcpClient.OnDataReceiveListener() {
        @Override
        public void onConnectSuccess() {
            Log.i(TAG,"onDataReceive connect success");
           // tv_state.setText("已连接");
        }

        @Override
        public void onConnectFail() {
            Log.e(TAG,"onDataReceive connect fail");
            //tv_state.setText("未连接");
        }

        @Override
        public void onDataReceive(byte[] buffer, int size, int requestCode) {
            byte[] data = new byte[size];
            System.arraycopy(buffer, 0, data, 0, size);

           // final String oxValue = Arrays.toString(HexUtil.Byte2Ox(data));
            Log.i(TAG,"buffer : " + buffer);

           // tv_receive.setText(tv_receive.getText().toString() + oxValue + "\n");

        }
    };



    @Override
    protected void onDestroy() {
        TcpClient.getInstance().disconnect();
        super.onDestroy();
    }







    public static Handler uiHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch(msg.what){
                //connect error
                case -1:
                    break;
                //connect success
                case 1:
                    break;

                //receive data
                case 100:
                    handleMsg(msg);
                   // textViewUpdate(msg);
                    break;
            }
        }
    };

    private static void handleMsg(Message recvmsg) {
        Bundle bundle = recvmsg.getData();
        String target = bundle.getString("target");
        String id = bundle.getString("id");
        String entity = bundle.getString("entity");
        Log.i(TAG,"recvmsg :" + recvmsg.toString());
        Log.i(TAG,"id :" + id + " target " + target + " entity " + entity);

        if (entity.equals("finsh") && target.equals("660") && id.equals("333"))
            m660IsFinish = true;

        if (entity.equals("finsh") && target.equals("9607") && id.equals("333"))
            m9607IsFinish = true;

        if (target.equals("660") && id.equals("222")) {
            TV660.setText(entity);
        }

        if (target.equals("9607") && id.equals("222")) {
            TV9607.setText(entity);
        }

    }




}
